package Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    StringBuilder url=new StringBuilder("jdbc:postgresql://43.139.106.233:5432/");
    String user="postgres";
    String password="123456";

    public Connection getConnection(String DBSName) throws Exception{
        Connection con=null;
        //尝试连接
        try{
            //加载驱动
            Class.forName("org.postgresql.Driver");
            //连接
            url.append(DBSName);
            con= DriverManager.getConnection(url.toString(),user,password);
            if(!con.isClosed()){
                System.out.println("连接成功");
            }
        }catch (ClassNotFoundException e){
            System.out.println("驱动装载失败");
        }catch (SQLException e){
            e.printStackTrace();
            System.out.println("连接失败");
        }
        return con;
    }
}
